package com.dsunsoft.rtu.core.oauth.config;

import com.dsunsoft.rtu.core.oauth.jwt.JwtTokenEnhancer;
import com.dsunsoft.rtu.properties.OAuthProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

/**
 * TokenStoreConfig
 *
 * @author ygm
 */
@Configuration
public class TokenStoreConfig {

	@Autowired
	private OAuthProperties oauthProperties;

	/**
	 * redis存储token
	 */
	@Bean
	public TokenStore tokenStore() {
		return new JwtTokenStore(jwtAccessTokenConverter());
	}

	/**
	 * 用于生成jwt
	 *
	 * @return
	 */
	@Bean
	public JwtAccessTokenConverter jwtAccessTokenConverter() {
		JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
		converter.setSigningKey(oauthProperties.getJwtSigningKey());// 生成签名的key
		return converter;
	}

	/**
	 * 用于扩展JWT
	 *
	 * @return
	 */
	@Bean
	public TokenEnhancer jwtTokenEnhancer() {
		return new JwtTokenEnhancer();
	}

//	/**
//	 * tokenServices 设置
//	 */
//	@Bean
//	@Primary
//	public DefaultTokenServices defaultTokenServices() {
//		DefaultTokenServices tokenServices = new DefaultTokenServices();
//		// 存放到 redis
//		tokenServices.setTokenStore(tokenStore());
//		tokenServices.setSupportRefreshToken(true);
//		// token有效期自定义设置
//		tokenServices.setAccessTokenValiditySeconds(oAuth2Properties.getExpiresIn());
//		// refresh_token默认7天
//		tokenServices.setRefreshTokenValiditySeconds(60 * 60 * 24 * 7);
//		return tokenServices;
//	}
}
